define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    // index_url: 'module/index/index',
                    // add_url: 'module/index/add',
                    // edit_url: 'module/index/edit',
                    // del_url: 'module/index/delete',
                    multi_url: 'module/index/multi',
                    table: 'module',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'identifier',
                sortName: 'identifier',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'name', title: __('Name'),align:'left'},
                        {field: 'identifier', title: __('Identifier'),align:'left'},
                        {field: 'author', title: __('Author'),align:'left'},
                        {field: 'desciption', title: __('Desciption'),align:'left'},
                        {field: 'email', title: __('Email'),align:'left'},
                        {field: 'siteurl', title: __('Siteurl'),visible: false},
                        {field: 'sort', title: __('Sort')},
                        {field: 'version', title: __('Version')},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'updatetime', title: __('Updatetime'), formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
            // Controller.api.bindevent();
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
            // Controller.api.bindevent();
        },
        config:function() {
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                operate: function (value, row, index) {
                    var _operate = '';
                    if(row.status > -1) {
                        _operate += '&nbsp;<a class="btn btn-info btn-xs btn-config">' + __('config') + '</a>';
                        _operate += '&nbsp;<a class="btn btn-danger btn-xs btn-uninstall">' + __('uninstall') + '</a>';
                    } else {
                        _operate += '&nbsp;<a class="btn btn-info btn-xs btn-install">' + __('install') + '</a>';
                    }
                    return _operate;
                }
            },
            events:{
                operate: $.extend({
                    'click .btn-install':function(e, value, row, index) {
                        Fast.api.ajax(Config.root + '/admin/module/install/identifier/' + row.identifier,function(data){
                            $('#table').bootstrapTable('refresh');
                            return true;
                        });
                    },
                    'click .btn-uninstall':function(e, value, row, index) {
                        Fast.api.ajax(Config.root + '/admin/module/uninstall/identifier/' + row.identifier, function(data){
                            $('#table').bootstrapTable('refresh');
                            return true;
                        });
                    },
                    'click .btn-config': function (e, value, row, index) {
                        Backend.api.open(Config.root + '/admin/module/config/identifier/' + row['identifier'], __('config'));
                    }
                }, Table.api.events.operate)
            }
        }
    };
    return Controller;
});